// $(function () {
//     findAll();
// });
layui.use(['laypage', 'layer'], function () {
    var laypage = layui.laypage
        , layer = layui.layer;

    let res = myAjax("/back/user/getCount", {});
    //完整功能
    laypage.render({
        elem: 'page'
        , count: res.count
        , layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip']
        , jump: function (obj) {

            let data = {page: obj.curr, limit: obj.limit};
            console.log(data)
            findAll(data);
        }
    });
});

function findAll(data) {
    //所有的查询都是用findAll（模糊查询和分页）
    let res = myAjax("/back/user/findAll", data);
    // console.log(res, "1111111111")
    setData(res.data);
}

function setData(data) {
    let html = '';
    for (let i = 0; i < data.length; i++) {
        html += '<tr>\n' +
            '                                    <td>\n' +
            '                                      <input type="checkbox" name="id" value="1"   lay-skin="primary"> \n' +
            '                                    </td>\n' +
            '                                    <td>' + data[i].id + '</td>\n' +
            '                                    <td>' + judgeNull(data[i].username) + '</td>\n' +
            '                                    <td>' + judgeNull(data[i].sex) + '</td>\n' +
            '                                    <td>' + judgeNull(data[i].phone) + '</td>\n' +
            '                                    <td>' + judgeNull(data[i].address) + '</td>\n' +
            '                                    <td>' + judgeNull(data[i].hobby) + '</td>\n' +
            '                                    <td>' + judgeNull(data[i].birthday) + '</td>\n' +
            '                                    <td class="td-status">\n' +
            '                                      <span class="layui-btn layui-btn-normal layui-btn-mini ' + (data[i].enable == 1 ? '' : 'layui-btn-disabled') + '">' + (data[i].enable == 1 ? '已启用' : '已停用') + '</span></td>\n' +
            '                                    <td class="td-manage">\n' +
            '                                      <a onclick="member_stop(this,\'' + data[i].id + '\')" href="javascript:;"  title="' + (data[i].enable == 1 ? '启用' : '停用') + '">\n' +
            '                                        <i class="layui-icon">' + (data[i].enable == 1 ? '&#xe601;' : '&#xe62f;') + '</i>\n' +
            '                                      </a>\n' +
            '                                      <a title="编辑"  onclick="edit(' + data[i].id + ')" href="javascript:;">\n' +
            '                                        <i class="layui-icon">&#xe642;</i>\n' +
            '                                      </a>\n' +
            '                                      <a onclick="xadmin.open(\'修改密码\',\'member-password.html\',600,400)" title="修改密码" href="javascript:;">\n' +
            '                                        <i class="layui-icon">&#xe631;</i>\n' +
            '                                      </a>\n' +
            '                                      <a title="删除" onclick="member_del(this,\'' + data[i].id + '\')" href="javascript:;">\n' +
            '                                        <i class="layui-icon">&#xe640;</i>\n' +
            '                                      </a>\n' +
            '                                    </td>\n' +
            '                                  </tr>';
    }
    $("#tbody").html(html);
}

function judgeNull(value) {
// console.log(value,typeof value)
    return value == undefined || value == 'null' ? '' : value;

}

function edit(id) {
    sessionStorage.setItem("userId", id);
    console.log(id);
    xadmin.open('编辑', '/html/user/member-edit.html', 600, 400)

}

layui.use(['laydate', 'form'], function () {
    var laydate = layui.laydate;
    var form = layui.form;


    // 监听全选
    form.on('checkbox(checkall)', function (data) {

        if (data.elem.checked) {
            $('tbody input').prop('checked', true);
        } else {
            $('tbody input').prop('checked', false);
        }
        form.render('checkbox');
    });

    //执行一个laydate实例
    laydate.render({
        elem: '#start' //指定元素
    });

    //执行一个laydate实例
    laydate.render({
        elem: '#end' //指定元素
    });


});

/*用户-停用*/
function member_stop(obj, id) {
    let title = $(obj).attr('title');
    layer.confirm('确认要' + (title == '启用' ? '停用' : '启用') + '吗？', function (index) {

        let res = myAjax("/back/user/enable", {id: id}, "post");
        if (res.count > 0) {
            if ($(obj).attr('title') == '启用') {

                //发异步把用户状态进行更改
                $(obj).attr('title', '停用')
                $(obj).find('i').html('&#xe62f;');

                $(obj).parents("tr").find(".td-status").find('span').addClass('layui-btn-disabled').html('已停用');
                layer.msg('已停用!', {icon: 5, time: 1000});

            } else {
                $(obj).attr('title', '启用')
                $(obj).find('i').html('&#xe601;');

                $(obj).parents("tr").find(".td-status").find('span').removeClass('layui-btn-disabled').html('已启用');
                layer.msg('已启用!', {icon: 1, time: 1000});
            }
        } else {
            layer.msg('停用失败!', {icon: 5, time: 1000});
        }

        // findAll({});
    });
}

/*用户-删除*/
function member_del(obj, id) {
    console.log(id, "-----------------------");
    layer.confirm('确认要删除吗？', function (index) {
        //发异步删除数据
        let res = myAjax("/back/user/del", {id: id}, "post");
        if (res.count > 0) {
            $(obj).parents("tr").remove();
            layer.msg('已删除!', {icon: 1, time: 1000});
        } else {
            layer.msg('删除失败!', {icon: 1, time: 1000});
        }

    });
}


function delAll(argument) {
    var ids = [];

    // 获取选中的id
    $('tbody input').each(function (index, el) {
        if ($(this).prop('checked')) {
            ids.push($(this).val())
        }
    });

    layer.confirm('确认要删除吗？' + ids.toString(), function (index) {
        //捉到所有被选中的，发异步进行删除
        layer.msg('删除成功', {icon: 1});
        $(".layui-form-checked").not('.header').parents('tr').remove();
    });
}